package com.xiaoyu.commonlib.ui.widget;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.CountDownTimer;
import android.support.annotation.RequiresApi;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xiaoyu.commonlib.R;


/**
 * @author jx
 * @date 创建时间 2018/7/17
 * @Description 发送验证码基类
 */
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public class SendVcodeItem extends RelativeLayout implements OnClickListener {

    private EditText mEditText;
    private TextView mSend;
    private static final int COUNTDOWN = 60;
    private CountDownTimer mCountDownTimer;

    private ISendVcodeListener mListener;

    public SendVcodeItem(Context context) {
        super(context);
        initView();
        initCountDownTimer();
//        startCountDown();
    }

    public SendVcodeItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        initCountDownTimer();
//        startCountDown();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.common_send_code_item, this, true);
        mEditText = findViewById(R.id.common_send_code_item_edit);
        mSend = findViewById(R.id.common_send_code_item_txt);
        mSend.setOnClickListener(this);
    }

    public EditText getCode() {
        return mEditText;
    }

    private void initCountDownTimer() {
        mCountDownTimer = new CountDownTimer(COUNTDOWN * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                int second = (int) (millisUntilFinished / 1000);
                mSend.setText(second + "s");
            }

            @Override
            public void onFinish() {
                stopCountDown();
            }
        };
    }

    @Override
    public void onClick(View v) {
        if (v == mSend) {
            if (mListener != null) {
                mListener.requestSendVcode();
            }
            startCountDown();
        }
    }


    //绿色
    public void stopCountDown() {
        mSend.setTextColor(Color.parseColor("#333333"));
        mSend.setText("验证");
        mSend.setBackground(this.getResources().getDrawable(R.drawable.bg_common_verify));
        mSend.setEnabled(true);
        mCountDownTimer.cancel();
    }

    //点击之后为绿色并有60s倒计时
    public void startCountDown() {
        mSend.setTextColor(Color.parseColor("#333333"));
        mSend.setBackground(this.getResources().getDrawable(R.drawable.bg_common_verify));
        mSend.setText(COUNTDOWN + "s");
        mSend.setEnabled(false);
        mCountDownTimer.start();
    }


    public void setHint(String hint) {
        mEditText.setHint(hint);
    }

    public String getEditContent() {
        return mEditText.getText().toString();
    }

    public interface ISendVcodeListener {
        public void requestSendVcode();
    }

    public void setSendVcodeListener(ISendVcodeListener listener) {
        mListener = listener;
    }

}
